/*
 *  Copyright 2011 Alexey Andreev.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.xthl.wiki;

import java.util.List;
import org.nop.templating.Template;
import org.nop.templating.XmlTreeBuilder;

/**
 *
 * @author Alexey Andreev
 */
public class DiffRenderer implements Template {
    private List<String> original;
    private List<String> patch;
    
    public DiffRenderer(List<String> original, List<String> patch) {
        this.original = original;
        this.patch = patch;
    }

    @Override
    public void generate(XmlTreeBuilder treeBuilder) {
        int lastCopied = 0;
        int index = 0;
        int originalLineNum = 1;
        int newLineNum = 1;
        while (index < patch.size()) {
            String[] parts = patch.get(index++).split(",");
            int start = Integer.parseInt(parts[0]);
            int length = Integer.parseInt(parts[1]);
            int newLength = Integer.parseInt(parts[2]);
            
            for (int i = lastCopied; i < start; ++i) {
                treeBuilder.addElement("tr");
                insertLine(treeBuilder, originalLineNum++, original.get(i));
                insertLine(treeBuilder, newLineNum++, original.get(i));
                treeBuilder.closeElement();
            }
            
            String className;
            if (length == 0) {
                className = "added";
            } else if (newLength == 0) {
                className = "deleted";
            } else {
                className = "replaced";
            }
            int top = Math.max(length, newLength);
            for (int i = 0; i < top; ++i) {
                treeBuilder.addElement("tr");
                treeBuilder.setAttribute("class", className);
                if (i < length) {
                    insertLine(treeBuilder, originalLineNum++, original.get(start + i));
                } else {
                    insertEmptyLine(treeBuilder);
                }
                if (i < newLength) {
                    insertLine(treeBuilder, newLineNum++, patch.get(index + i));
                } else {
                    insertEmptyLine(treeBuilder);
                }
                treeBuilder.closeElement();
            }
            
            index += newLength;
            lastCopied = start + length;
        }
        
        for (int i = lastCopied; i < original.size(); ++i) {
            treeBuilder.addElement("tr");
            insertLine(treeBuilder, originalLineNum++, original.get(i));
            insertLine(treeBuilder, newLineNum++, original.get(i));
            treeBuilder.closeElement();
        }
    }
    
    private void insertLine(XmlTreeBuilder treeBuilder, int lineNumber, String content) {
        treeBuilder.addElement("td");
        treeBuilder.setAttribute("class", "lineNumber");
        treeBuilder.addText(String.valueOf(lineNumber));
        treeBuilder.closeElement();
        treeBuilder.addElement("td");
        treeBuilder.addElement("pre");
        treeBuilder.addText(content);
        treeBuilder.closeElement();
        treeBuilder.closeElement();
    }
    
    private void insertEmptyLine(XmlTreeBuilder treeBuilder) {
        treeBuilder.addElement("td");
        treeBuilder.setAttribute("class", "lineNumber");
        treeBuilder.closeElement();
        treeBuilder.addElement("td");
        treeBuilder.closeElement();
    }
}
